//
//  LBCReadingViewController.m
//  喜马拉雅改良版框架
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LBCReadingViewController.h"

@interface LBCReadingViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, ReadHeadViewDelegate>

@property (nonatomic, retain)UICollectionView *readingCollectionView;
@property (nonatomic, retain)NSMutableArray *readListArray;
@property(nonatomic,retain)NSMutableArray *headArray;

@end

@implementation LBCReadingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"阅读";
    
    //初始化
    self.headArray = [NSMutableArray array];
    
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    // 设置每个item(cell)的大小
    flowLayout.itemSize = CGSizeMake((SCREEN_WIDTH - 24) / 3, (SCREEN_WIDTH - 24) / 3);
    // 设置最小行间距
    flowLayout.minimumLineSpacing = 4;
    // 设置最小列间距
    flowLayout.minimumInteritemSpacing = 4;
    // 设置item与四周边界的距离  上左下右
    flowLayout.sectionInset = UIEdgeInsetsMake(8, 8, 8, 8);
    // 设置头部区域大小
    flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT / 3.7);
    
    _readingCollectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
    [self.view addSubview:_readingCollectionView];
    _readingCollectionView.backgroundColor = [UIColor whiteColor];
    _readingCollectionView.delegate = self;
    _readingCollectionView.dataSource = self;
    
    [self.readingCollectionView registerClass:[LBCReadListCell class] forCellWithReuseIdentifier:@"readCell"];
    [self.readingCollectionView registerClass:[LBCReadHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    self.readListArray = [NSMutableArray array];
    [self getAFNetWorkingData];
    
}

#pragma mark- 数据解析
- (void)getAFNetWorkingData{
    NSString *urlStr = @"http://api2.pianke.me/read/columns";
    NSString *para = @"auth=&client=1&deviceid=DC7311B8-4DFE-4BC2-99D9-565BF1215670&version=3.0.6";
    [AFNetworkTool postUrl:urlStr body:para result:HXJSON requestStyle:HXRequestString headerFile:nil success:^(id result) {
    
        NSMutableDictionary *bigDic = [result objectForKey:@"data"];
        NSMutableArray *array = [bigDic objectForKey:@"list"];
        for (NSMutableDictionary *dic in array) {
            LBCReadList *readList = [LBCReadList new];
            [readList setValuesForKeysWithDictionary:dic];
            [_readListArray addObject:readList];
        }
        _headArray = [bigDic objectForKey:@"carousel"];
        [_readingCollectionView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
}


#pragma mark -- 设置头部和底部区域内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        // 创建头部区域view
        LBCReadHeadView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        headView.readArray = self.headArray;
        headView.readHeadDelegate = self;
        
        return headView;
    }
    return nil;
}


#pragma mark -- 设置显示Item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _readListArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *readCellIdntifier = @"readCell";
    LBCReadListCell *readCell = [collectionView dequeueReusableCellWithReuseIdentifier:readCellIdntifier forIndexPath:indexPath];
    
    
    readCell.readListCell = _readListArray[indexPath.row];
    
    return readCell;
}
#pragma mark -- 点击item触发的方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    LBCDetailListViewController *detailListVC = [LBCDetailListViewController new];
    detailListVC.readListScrollV = _readListArray[indexPath.item];
    [self.navigationController pushViewController:detailListVC animated:YES];
}

-(void)presentToViewControllerContentId:(NSString *)contentId{
    LBCDetailViewController *detailVC = [LBCDetailViewController new];
    detailVC.contentId = contentId;
    [self.navigationController pushViewController:detailVC animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
